const { promisify } = require('util');
const download = promisify(require('download-git-repo'));
const path = require('path');

// 模板地址
const { reacttmp } = require('../config/temp-git-path')
// 执行cmd指令
const { commandSpawn } = require('../utils/terminal')
// 编译模板
const { compile, writeToFile, createDir } = require('../utils/utils')

// callback -> promisify(函数) -> Promise -> async await
// project 所创建的文件名
// 创建项目
const createAction = async (project) => {
    console.log("正在构建项目中~")
    // 远程克隆react的模板
    await download(reacttmp, project, { clone: true })
    // 初始化依赖下载 判断电脑配置环境
    const command = process.platform === 'win32' ? 'yarn.cmd' : 'yarn';
    await commandSpawn(command, ['install'], { cwd: `./${project}` })
    //运行项目 执行npm run start
    commandSpawn(command, ['start'], { cwd: `./${project}` });
}

// 创建组件
const addComponentAction = async (name, dest) => {
    // 获取编译成功后的模板内容
    const component = await compile("react-component.ejs", { name, wrapperName: name + 'Wrapper' })
    const style = await compile("react-style.ejs", { wrapperName: name + 'Wrapper' })
    // 写入文件的操作
    const targetDest = path.resolve(dest, name.toLowerCase())
    if (createDir(targetDest)) {
        const componentPath = path.resolve(targetDest, `index.tsx`);
        const stylePath = path.resolve(targetDest, `style.ts`);
        writeToFile(componentPath, component);
        writeToFile(stylePath, style);
    }
}

// 创建页面
const addPageAndRouteAction = async (name, dest) => {
    // 获取编译成功后的模板内容
    const pageTmp = await compile("react-page.ejs", { name, wrapperName: name + 'Wrapper' })
    const styleTmp = await compile("react-style.ejs", { wrapperName: name + 'Wrapper' })
    // 写入文件的操作
    const targetDest = path.resolve(dest, name.toLowerCase())
    if (createDir(targetDest)) {
        const pagePath = path.resolve(targetDest, `index.tsx`);
        const stylePath = path.resolve(targetDest, `style.ts`);
        writeToFile(pagePath, pageTmp);
        writeToFile(stylePath, styleTmp);
    }

}

// 创建store模块
const addStoreAction = async (module, dest) => {
    // 获取编译成功后的模板内容
    const actionTmp = await compile("store/redux-action.ejs", {})
    const contantTmp = await compile("store/redux-contant.ejs", {})
    const indexTmp = await compile("store/redux-index.ejs", {})
    const reducerTmp = await compile("store/redux-reducer.ejs", {})
    // 写入文件的操作
    const targetDest = path.resolve(dest, module.toLowerCase())
    if (createDir(targetDest)) {
        const actionPath = path.resolve(targetDest, `action.ts`);
        const contantPath = path.resolve(targetDest, `contant.ts`);
        const indexPath = path.resolve(targetDest, `index.ts`);
        const reducerPath = path.resolve(targetDest, `reducer.ts`);
        writeToFile(actionPath, actionTmp);
        writeToFile(contantPath, contantTmp);
        writeToFile(indexPath, indexTmp);
        writeToFile(reducerPath, reducerTmp);
    }
}

// 创建store模块
const addServiceAction = async (module, dest) => {
    // 获取编译成功后的模板内容
    const serverTmp = await compile("react-server.ejs", {})
    // 写入文件的操作
    const targetDest = path.resolve(dest, module.toLowerCase())
    if (createDir(targetDest)) {
        const serverPath = path.resolve(targetDest, `index.ts`);
        writeToFile(serverPath, serverTmp);

    }
}

module.exports = {
    createAction,
    addComponentAction,
    addPageAndRouteAction,
    addStoreAction,
    addServiceAction
}